import json
import urllib.request

def getLatOrLng(tags, refKey, tudeKey):
    """获取经度或纬度"""
    if refKey not in tags:
        return None
    ref = tags[refKey].printable
    LatOrLng = tags[tudeKey].printable[1:-1].replace(" ","").replace("/",",").split(",")
    LatOrLng = float(LatOrLng[0])+float(LatOrLng[1])/60+float(LatOrLng[2])/float(LatOrLng[3])/3600
    if refKey == 'GPS GPSLatitudeRef' and tags[refKey].printable != "N":
        LatOrLng = LatOrLng*(-1)
    if refKey == 'GPS GPSLongitudeRef' and tags[refKey].printable != "E":
        LatOrLng = LatOrLng*(-1)
    return LatOrLng

def getlocation(lat, lng):
    """调用百度地图API获取位置信息"""
    url = f'http://api.map.baidu.com/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak=申请的百度地图KEY'
    req = urllib.request.urlopen(url)
    res = req.read().decode("utf-8") 
    str_data = json.loads(res)
    jsonResult = str_data.get('result')
    formatted_address = jsonResult.get('formatted_address')
    return formatted_address 